package br.com.linkcom.sined.geral.dao;

import java.util.HashMap;
import java.util.List;

import br.com.linkcom.neo.core.standard.Neo;
import br.com.linkcom.sined.geral.bean.Tela;
import br.com.linkcom.sined.util.neo.persistence.GenericDAO;

public class TelaDAO extends GenericDAO<Tela> {
	
	protected HashMap<String, String> mapTelas;
	public static HashMap<String, String> mapTelasFromPathsProperties;
	
	public String getTelaDescriptionByUrl(String url) {
		String descricao = null;
		if (mapTelasFromPathsProperties!=null && mapTelasFromPathsProperties.get(url)!=null){
			descricao = mapTelasFromPathsProperties.get(url);
		}else if (mapTelas!=null && mapTelas.get(url)!=null){
			descricao = mapTelas.get(url);
		}
				
		if (descricao==null){
			List<Tela> telas = findAll();
			HashMap<String, String> mapa = new HashMap<String, String>();
			for (Tela tela : telas) {
				mapa.put(tela.getPath(), tela.getDescricao());
			}
			mapTelas = mapa;
			descricao = mapTelas.get(url);
		}
		
		return descricao;
	}
		
	public void clearTelaCache(){
		mapTelas = null;
	}
	
	/* singleton */
	private static TelaDAO instance;
	public static TelaDAO getInstance() {
		if (instance==null){
			instance = Neo.getObject(TelaDAO.class);
		}
		return instance;
	}
}